package com.css.apps.base.org.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.org.model.SSysOrgTree;
import com.css.apps.base.sys.action.DelSys;
import com.css.apps.base.sys.model.SSys;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * 删除自定义组织机构树
 * 
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class DelCustomOrgTree extends AdminAction {

	private static Log log = LogFactory.getLog(DelSys.class);
	private String ids;

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(ids)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			List<String> listIds = StringHelper.strToList(ids);
			List<SSysOrgTree> sysList = QueryCache.idToObj(SSysOrgTree.class, listIds);
			tx = new TransactionCache();
			for (SSysOrgTree systree : sysList) {
				tx.delete(systree);
			}
			tx.commit();

			for (SSysOrgTree systree : sysList) {
				LogPart lp = new LogPart();
				lp.setOpObjType(SSys.class.getName());
				lp.setOpObjId(systree.getUuid());
				lp.setRelObjType(SSys.class.getName());
				lp.setRelObjId(systree.getUuid());
				lp.setOpType(LogConstant.LOG_TYPE_DELETE);
				lp.setLogLevel(LogConstant.LOG_LEVEL_COMMON);
				lp.setLogData("");
				lp.setResult(LogConstant.RESULT_SUCCESS);
				lp.save();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
